#include "string.h"
#include "global.h"
#include "debug.h"

void memset(void* dst_, uint8_t value, uint32_t size){
    ASSERT(dst_ != NULL);
    uint8_t* dst = (uint8_t*) dst_;
    while(size-->0)*dst++ = value;
}

void memcpy(void* dst_, const void* src_, uint32_t size){
    ASSERT(dst_ != NULL && src_ != NULL);
    uint8_t* dst = dst_;
    const uint8_t* src = src_;
    while(size-->0)*dst++=src++;
}

int memcmp(const void* a_, const void* b_, uint32_t size){
    const char* a = a_;
    const char* b = b_;
    ASSERT(a != NULL && b != NULL);
    while(size-->0){if(*a!=*b){return*a>*b?1:-1;} ++a;++b; }
}

char* strcpy(char* dst_, char* src_){
    ASSERT(dst_ != NULL && src_ != NULL);
    char* r = dst_;
    while( *dst_++ = *src_++ );
    return r;
}

char* strlen(const char* str){
    ASSERT(str != NULL);
    const char* p = str;
    while(*p++);
    return p-str-1;
}

uint8_t* strcmp(const char* a, const char* b){
    ASSERT(a != NULL && b != NULL);
    while(*a!=0 && *a == *b){++a;++b;}
    return *a<*b? -1: *a>*b;
}

// find first occurrence of 'ch' in 'str'
char* strchr(const char* str, const uint8_t ch){
    ASSERT(str != NULL);
    while(*str){ if(*str==ch){return (char*)str;} str++; }
    return NULL;
}

// find last occurremce of 'ch' in 'str'
char* strrchr(const char* str, const uint8_t ch){
    ASSERT(str != NULL);
    const char* last_char = NULL;
    while(*str){ if(*str==ch){last_char = str;} str++; }
    return last_char;
}

// concat 'src_' after 'dst_'
char* strcat(const char* dst_, const char* src_){
    ASSERT(dst_ != NULL && src_ != NULL);
    char* str = dst_; while(*str++); --str;
    while(*str++=*src_++);
    return dst_;
}

// #occurence of 'ch' in 'str'
uint32_t strchrs(const char* str, const uint8_t ch){
    ASSERT(str != NULL);
    uint32_t cnt = 0;
    const char* p = str;
    while(*p){ if(*p==ch){cnt++;} p++; }
    return cnt;
}




